"""


"""
from goods.models import GoodsChannel


def categories_loader():

    categories = dict()

    channels = GoodsChannel.objects.order_by("group_id", "sequence")

    for channel in channels:

        # 创建 "1":{}字典
        if channel.group_id not in categories:
            # 创建"channels":[] 列表
            categories[channel.group_id] = {"channels":list(), "sub_cats": list()}

        channel_dict = dict()
        channel_dict["id"] = channel.category.id
        channel_dict["name"] = channel.category.name
        channel_dict["url"] = channel.url

        categories[channel.group_id]["channels"].append(channel_dict)

        # 创建 "sub_cats":[] 列表
        sub_cates = channel.category.subs.all()

        for sub_cate in sub_cates:
            sub_dict = dict()
            sub_dict["id"] = sub_cate.id
            sub_dict["name"] = sub_cate.name
            sub_dict["sub_cats"] = list()

            sub_cates2 = sub_cate.subs.all()
            for sub_cate2 in sub_cates2:
                sub_dict2 = {
                    "id": sub_cate2.id,
                    "name": sub_cate2.name
                }
                sub_dict["sub_cats"].append(sub_dict2)

            categories[channel.group_id]["sub_cats"].append(sub_dict)

    return categories
